package cn.kmsoft.jssc.config;

import com.alibaba.druid.filter.logging.Log4jFilter;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

/**
 * 程序的主配置
 */
@Configuration
public class AppConfig {
    @Autowired
    private Environment env;

    @Bean
    public Log4jFilter log4jFilter(){
        Log4jFilter filter = new Log4jFilter();
        filter.setResultSetLogEnabled(false);
        filter.setStatementSqlPrettyFormat(false);
        filter.setStatementExecutableSqlLogEnable(true);
        //
        filter.setDataSourceLogEnabled(false);
        filter.setConnectionLogEnabled(false);
        filter.setStatementLogEnabled(false);
        filter.setResultSetLogEnabled(false);

        return filter;
    }

//    @Bean
//    public ServletWebServerFactory servletWebServerFactory(){
//        return new TomcatServletWebServerFactory();
//    }


    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "cqtky");
        reg.addInitParameter("loginPassword", "cqtky");
        reg.addInitParameter("logSlowSql", "true");
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");

        return filterRegistrationBean;
    }

}
